<?php
namespace app\vote\controller;

use app\vote\Model\Admin;
use think\Validate;
use think\captcha\Captcha;
use think\Cookie;
use think\Session;


class RootLogin extends \think\Controller
{
    public function _initialize(){
        if(Session::has('user') and Session::has('password')){
            $this->redirect('http://www.tp5.com/vote/Root/home');
        }
    }

    public function login(){
        return view();
    }
    
    public function doLogin(){
        $request=request()->param();
        $rules = [
            'userName'  => 'require|max:11',
            'passWord'   => 'require|max:25',
        ];
        $msg = [
            'userName.require' => '账号必填',
            'userName.max'     => '账号最多不能超过11个字符',
            'passWord.require'   => '密码必填',
            'passWord.max'  => '密码最多不超过25个字符',
        ];
        $validate = new Validate($rules,$msg);
        $result=$validate->check($request);
        if(!$result){
            echo $validate->getError();
            $this->error('操作失败','http://www.tp5.com/vote/RootLogin/Login',3);
        }else{
            $id=$request['userName'];
            $pwd=$request['passWord'];
            $root=new Admin();
            $find=$root->find($id,$pwd);
            if($find=="SUCCESS"){
                $this->success('登录成功','Root/home',3);

            }elseif($find=="ERROR PASSWORD"){


            }else{


            }
            // if(!isset($request['remember'])){                 
            //     $table='root';
            //     $user=new Admin();
            //     $user->find($table,$id,$pwd);
            // }else{
            //     // 设置cookie 前缀为think_
            //         Session::set('user',"$id");
            //         Session::set('password',"$pwd");
            //         Cookie::set("user","$id",['prefix'=>'think_','expire'=>3600]);
            //         Cookie::set("password","$pwd",['prefix'=>'think_','expire'=>3600]);
            //         $table='root';
            //         $user=new Admin();
            //         $user->find($table,$id,$pwd);
            // }    
        }
       

    }

   


    
}   


